#include <iostream>
#include <time.h>
#include <stdlib.h>
#include "Set.hpp"
#include "Map.hpp"
using namespace std;

///void Test1(){
///  //int arr[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
///  int arr[] = {1,2,3,4,5,6,7,8,9,10};
///  RBTree<int, int> rbt;
///  for(auto e : arr)
///  {
///    rbt.Insert(make_pair(e, e));
///  }
///  rbt.Inorder();
///  cout << "Isbalance: " << rbt.Isbalance() << endl;
///}
///
///void Test2(){
///  srand(time(NULL));
///  RBTree<int, int> rbt;
///  const int N = 10000;
///  for(int i = 0; i<N; ++i)
///  {
///    int x = rand();
///    rbt.Insert(make_pair(x, i));
///  }
///  cout << "Isbalance: " << rbt.Isbalance() << endl;
///}

void Test1(){
  zty::set<int> s;
  s.Insert(3);
  s.Insert(2);
  s.Insert(6);
  s.Insert(7);
  s.Insert(9);
  //s.Inorder();
  zty::set<int>::iterator sit = s.begin();
  while(sit != s.end())
  {
    cout << *sit << " ";
    ++sit;
  }
  cout << endl;

  zty::map<int, int> m;
  m.Insert(make_pair(30, 3));
  m.Insert(make_pair(20, 2));
  m.Insert(make_pair(60, 6));
  m.Insert(make_pair(70, 7));
  m.Insert(make_pair(90, 9));
  //m.Inorder();
  auto mit = m.begin();
  while(mit != m.end())
  {
    cout << mit->first << ":" << mit->second << " ";
    ++mit;
  }
  cout << endl;
}

void WordCounter(){
  zty::map<string, int> counter;
  string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉"};
    
  //方法二：operator[]（推荐）
  for(string &e : arr)
  {
    //如果e不在counter中，先插入pair(e,int())，再对返回value(次数)++;
    //如果e在counter中，返回value(次数)的引用，次数++;
    ++counter[e];
  }
  for(auto &e : counter)
  {
    cout << e.first << ":" << e.second << endl;
  }
}
void Dictionary(){
  zty::map<string, string> dict;
  //map存储的元素是键值对pair，有以下几种pair的构造插入方法：
  //1.匿名对象构造法
  dict.Insert(pair<string, string>("string", "字符串"));
  //2.typedef类型重命名，缩短类型名
  typedef pair<string, string> DictKV;
  //3.通过make_pair函数模版构造键值对（推荐）
  dict.Insert(DictKV("peach", "桃子"));    
  dict.Insert(make_pair("dictionary", "字典"));    
  dict.Insert(make_pair("temporary", "短暂的"));
  dict.Insert(make_pair("declaration", "声明"));    
    
  //map的迭代器遍历
  //map的迭代器底层是指向pair键值对结构的指针
  //map<string, string>::iterator it = dict.begin();                   
  auto it = dict.begin(); //map迭代器的类型过长，通过auto自动识别类型    
  while(it != dict.end())                        
  {    
    //cout << (*it).first << ":" << (*it).second << endl; //解引用后通过.访问pair的成员    
    cout << it->first << ":" << it->second << endl;//直接通过->访问pair的成员（推荐）  
    ++it;    
  } 
   
  //范围for:
  //for(pair<const string, string> &e : dict) //map存储的元素是键值对pair，注意key是const类型
  for(auto &e : dict) //临时变量e应该取引用，避免发生拷贝构造，浪费资源   
  {    
    cout << e.first << ":" << e.second << endl; 
  }
    
  string input;
  while(cin >> input)                     
  {
    auto ret = dict.Find(input); //传入key值，返回对应元素(pair)的迭代器
    if(ret != dict.end()) //找不到返回map::end
    {
      cout << ret->first << ":" << ret->second << endl;
    }
    else{
      cout << "There is no such word!" << endl;
    }
  }
}



int main(){
  //Test1();
  //WordCounter();
  Dictionary();
  return 0;
}
